package com.da.javatest.leetCode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenlida
 * @date 2019/1/16 16:50
 * @description
 */
public class Code228 {
    public List<String> summaryRanges(int[] nums) {
        List<String> result = new ArrayList<>();
        if (nums.length < 1) {
            return result;
        }
        int start = -1;
        for (int i = 0; i < nums.length - 1; i++) {
            int val = nums[i + 1] - nums[i];
            if (val == 1 && start == -1) {
                start = nums[i];
            }
            if (val != 1 && start == -1) {
                result.add(nums[i] + "");
            }
            if (val != 1 && start != -1) {
                result.add(start + "->" + nums[i]);
                start = -1;
            }
        }
        if (start != -1) {
            result.add(start + "->" + nums[nums.length - 1]);
        } else {
            result.add(nums[nums.length - 1] + "");
        }
        return result;
    }
}
